/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tj.eskhata.pos.web.output;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import tj.eskhata.pos.domain.Client;
import tj.eskhata.pos.domain.User;
import tj.eskhata.pos.services.PosService;


/**
 * implementation of IDataProvider for contacts that keeps track of sort information
 * 
 * @author igor
 * 
 */
public class SortableContactDataProvider extends SortableDataProvider<Client>
{
	@SpringBean
	private PosService service;
	private String name;
	private String pasp;
	/**
	 * constructor
	 */
	public SortableContactDataProvider()
	{
		 InjectorHolder.getInjector().inject(this);
		// set default sort
		//setSort("name", true);
		this.name="";
		this.pasp="";
	}
	public SortableContactDataProvider(String name,String pasp)
	{
		 InjectorHolder.getInjector().inject(this);
		this.name=name;
		this.pasp=pasp;
		//setSort("name", true);
	}
	

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
	 */
	public Iterator<Client> iterator(int first, int count)
	{
		//SortParam sp = getSort();
		//if (name=="") 
		//	return service.findAllClients().subList(0, 20).iterator();
		List<Client> lst=service.criteriaByNameClient(name,pasp,30);
		int n=lst.size()>30?30:lst.size();
		return lst.subList(first, first+n).iterator();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size()
	{
		int i=30;
		return i<30?i:30;
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Client> model(final Client object)
	{
		return new AbstractReadOnlyModel<Client>() {
			@Override
			public Client getObject() {
				return object;
			}
		};
		//return new DetachableContactModel(object);
	}

}
